<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/13
 * Time: 10:59
 */

namespace app\mobile\controller;


use app\Admin\controller\Goods;
use app\Model\AdModel;
use app\Model\CommodityClassifyModel;
use app\Model\GoodsModel;
use app\Model\PositionModel;
use think\Controller;
use think\Db;

//手机端首页
class Index extends Controller
{

//    public function index()
//    {
//        $Banner = AdModel::field('*')
//            ->where('pid',1)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(5)
//            ->select();//首页顶部轮播图
//        $Recommended = GoodsModel::field('*')
//            ->where('is_recommend',1)
//            ->order('sort')
//            ->limit(9)
//            ->select();//精品推荐
//        $Newproducts = GoodsModel::field('*')
//            ->where('is_new',1)
//            ->order('sort')
//            ->limit(9)
//            ->select();//新品上市
//        $Sellwell = GoodsModel::field('*')
//            ->where('is_hot',1)
//            ->order('sort')
//            ->limit(9)
//            ->select();//热销商品
//        $Swbelowad = AdModel::field('*')
//            ->where('pid',101)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(5)
//            ->select();//热销商品下方的广告位
//        $ad_one = AdModel::field('*')
//            ->where('pid',151)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(1)
//            ->find();//广告位1
//        $ad_two = AdModel::field('*')
//            ->where('pid',152)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(1)
//            ->find();//广告位2
//        $ad_three = AdModel::field('*')
//            ->where('pid',153)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(1)
//            ->find();//广告位3
//        $ad_four = AdModel::field('*')
//            ->where('pid',154)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(1)
//            ->find();//广告位4
//        $ad_five = AdModel::field('*')
//            ->where('pid',155)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(1)
//            ->find();//广告位5
//        $ad_six = AdModel::field('*')
//            ->where('pid',156)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(1)
//            ->find();//广告位5
//        $ad_seven = AdModel::field('*')
//            ->where('pid',157)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(1)
//            ->find();//广告位5
//        $ad_eight= AdModel::field('*')
//            ->where('pid',158)
//            ->where('enabled',1)
//            ->order('orderby')
//            ->limit(1)
//            ->find();//广告位5
//        $this->assign('ad_one',$ad_one);
//        $this->assign('ad_two',$ad_two);
//        $this->assign('ad_three',$ad_three);
//        $this->assign('ad_four',$ad_four);
//        $this->assign('ad_five',$ad_five);
//        $this->assign('ad_six',$ad_six);
//        $this->assign('ad_seven',$ad_seven);
//        $this->assign('ad_eight',$ad_eight);
//        $Search = Db::name('tp_config')->select();//热门搜索
//        foreach ($Search as $k=>$v) {
//            if ($v['name'] == 'hot_keywords') {
//                $val = explode('|', $v['value']);
//                $this->assign('Search', $val);
//            }
//        }
//        $this->assign('Swbelowad',$Swbelowad);//热销商品下方的广告位
//        $this->assign('Sellwell',$Sellwell);//热销商品
//        $this->assign('Newproducts',$Newproducts);//新品上市
//        $this->assign('Recommended',$Recommended);//精品推荐
//        $this->assign('Banner',$Banner);//首页顶部轮播图
//        return view('index');
//    }
/*
 * 浏览器判断
 */
    public function llqif()
    {
        if(is_weixin()){//微信内部浏览器
            $this->redirect('Mobilewx/Index/index');
//            echo "这是微信内部浏览器";

        }else{//其他浏览器
            if (isMobile()){
//                echo "这是手机端";
                $a='手机';
            }
            else{
                $a='电脑';
//                echo "这是电脑端";
            }
        }
    }
    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *   首页数据加载
     */
    public function index()
    {
        $this->llqif();

        //茶品种--顶级分类
        $data['Comm1'] =CommodityClassifyModel::where('is_show','=',1)->where('delete_time',null)->where('parent_id','=',0)->order('sort_order','asc')->limit(4)->select();
        //首页顶部轮播图
        $data['Banner'] = AdModel::field('*')->where('pid',1)->where('enabled',1)->order('orderby')->limit(5)->select();
        //精品推荐
        $data['Recommended'] = GoodsModel::field('*')->where('is_on_sale',1)->where('is_recommend',1)->order('sort')->limit(9)->select();
        //新品上市
        $data['Newproducts'] = GoodsModel::field('*')->where('is_on_sale',1)->where('is_new',1)->order('sort')->limit(9) ->select();
        //热销商品
        $data['Sellwell'] = GoodsModel::field('*')->where('is_on_sale',1)->where('is_hot',1)->order('sort')->limit(9)->select();
        //其他
        $data['qita']=GoodsModel::field('*')->where('is_on_sale',1)->order('sort')->limit(9)->select();
      //热销商品上方的广告位1234(暂时无用)
        $data['ad_one'] = AdModel::field('*')  ->where('pid',151)->where('enabled',1)->order('orderby')->limit(1)->find();
        $data['ad_two'] = AdModel::field('*')  ->where('pid',152)->where('enabled',1)->order('orderby')->limit(1) ->find();//广告位2
        $data['ad_three'] = AdModel::field('*')->where('pid',153)->where('enabled',1)->order('orderby')->limit(1)->find();//广告位3
        $data['ad_four'] = AdModel::field('*') ->where('pid',154)->where('enabled',1)->order('orderby')->limit(1)->find();//广告位4

        //热销商品上方活动位
        //1.是否开启该活动板块
        $Position=PositionModel::find(2);
        if ($Position['is_open']==1){//开
            $huodong = AdModel::field('*')->where('pid',2)->where('enabled',1)->order('orderby')->limit(2)->select();//热销商品上方活动
            if ($huodong){//有设置活动
                $data['huo_dong']=$huodong;
                if ($data['huo_dong']->count()>=2){$data['hd_count']=2;}//有2个或以上
                else                              {$data['hd_count']=1;}
            }
        }
//        dump($data['huo_dong']->toArray());exit();

        $Search = Db::name('tp_config')->select();//热门搜索
        foreach ($Search as $k=>$v) {
            if ($v['name'] == 'hot_keywords') {
                $val = explode('|', $v['value']);
                $this->assign('Search', $val);
            }
        }
        return view('index3',$data);
    }

    /**
     *加载更多
     */
    public function ajaxGetMore(){
        $p = input('p');
        if ($p === 'undefined'){
            $p =1;
        }
        $favourite_goods = GoodsModel::field('*')
            ->where('is_recommend',1)
            ->where('is_on_sale',1)
            ->order('goods_id desc')
            ->page($p,10)
            ->select();
        $this->assign('favourite_goods',$favourite_goods);
//        $this->display();
        if ($favourite_goods){
            $num = array();
            foreach ($favourite_goods as $k=>$v){
                $data ='<li><a onclick="goodsinfo('.$v['goods_id'].')" title="'.$v['goods_name'].'"><div class="index_pro">';
                $data.='<div class="products_kuang">';
                $data.='<img src="'.goods_thum_images($v['goods_id'],400,400).'"></div>';
                $data.='<div class="goods_name">'.$v['goods_name'].'</div>';
                $data.='<div class="price">';
                $data.='<a onclick="goodsinfo('.$v['goods_id'].')" class="btns">';
                $data.='<img src="/assets/img/index_flow.png">';
                $data.='</a><span onclick="goodsinfo('.$v['goods_id'].')" class="price_pro">￥'.$v['shop_price'].'元</span>';
                $data.='</div></div></a></li>';
                $num[] = $data;
            }
        }

        return $num;
    }

    public function aaa(){
        goods_thum_images(95,20,200);
    }
    /*
     * 客服页
     */
    public function service()
    {
        return view('service');
    }
    public function service_die()
    {
        return view('service_die');
    }
}
